package com.cyanite.playlv2;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2.widget.SegmentedButton;
import com.cyanite.playlv2.widget.SegmentedButton.OnClickListenerSegmentedButton;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;


public class TripCreateMainActivity extends Activity    {

	private ImageView mImageTrip;
	private SegmentedButton mHeaderButton;
	private Button mBtnCreate;
	private int mImageIndex = 0;
	private AlphaAnimation mAnimation;

	private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

	public void onCreate(Bundle savedInstanceState)    {
		super.onCreate(savedInstanceState);
		registerReceiver(mLoggedOutReceiver, new IntentFilter(PlayLv2.INTENT_ACTION_LOGGED_OUT));
        setContentView(R.layout.trip_create_main_activity);

        mAnimation=new AlphaAnimation(0.0f, 0.8f);
        mAnimation.setDuration(1000);
        mAnimation.setInterpolator(new AccelerateDecelerateInterpolator());

        mBtnCreate = (Button)findViewById(R.id.btnCreate);
        mBtnCreate.getBackground().setAlpha(204);
        mBtnCreate.startAnimation(mAnimation);

        mBtnCreate.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
               if (mAnimation.hasEnded())   {
                    switch ( mImageIndex ) {
                        case 0:
                            openCreateActivity(TripCreateSceneryActivity.class);
                            break;
                        case 1:
                            openCreateActivity(TripCreateThemeActivity.class);
                            break;
                         default:
                             break;
                    }
                }
            }
        });

        mImageTrip = (ImageView)findViewById(R.id.imgTripNote);
        mImageTrip.setImageResource(R.drawable.trip_scenery);

        mHeaderButton = (SegmentedButton)findViewById(R.id.btnTripsSegmented);
        mHeaderButton.clearButtons();
        mHeaderButton.addButtons(
                getString(R.string.trip_normal_note),
                getString(R.string.trip_theme_note));

        mHeaderButton.setOnClickListener(new OnClickListenerSegmentedButton() {
            public void onClick(int index) {
                mImageIndex = index;
                mBtnCreate.startAnimation(mAnimation);
                switch(index)   {
                    case 0:
                        mImageTrip.setImageResource(R.drawable.trip_scenery);
                        break;
                    case 1:
                        mImageTrip.setImageResource(R.drawable.trip_theme);
                        break;
                    default:
                        break;
                }
            }
        });
	}

	@Override
    public void onPause() {
        super.onPause();
        if (isFinishing()) {
            unregisterReceiver(mLoggedOutReceiver);
        }
    }

	public boolean onCreateOptionsMenu(Menu menu)	{
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item)	{
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)	{
	    if (keyCode == KeyEvent.KEYCODE_BACK){
	        finish();
			return true;
	    }
	    return false;
	}

	private void openCreateActivity(Class<?> cls)	{
	    startActivity(new Intent(TripCreateMainActivity.this, cls));
	    finish();
	}

}
